<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class School_Years extends Backend_Controller {

	protected $_title = 'Tahun Ajaran';

	public function __construct() {
		parent::__construct();
		if(parent::get_actor_login()!="admin")
			redirect('login/index/1');
		$this->load->model('school_years_model', 'school_years', true);
	}
		
	public function index($offset=0, $sort_by='name') {
		
		$data = array(
			'mainview' => 'index',
		) + $this->_get_header_data();
		
		$this->load->view('admin/template',$data);
		
	}
	
	function _crud_output($output = null) {
        $this->load->view('admin/template_crud.php',$output);    
    }

	function school_years_display($operation=null) 	{	
		$crud = new grocery_CRUD();
        $crud->set_theme('flexigrid');
        $crud->set_table('school_years');
        $crud->set_subject('Tahun Ajaran');
        
        $crud->columns('school_year','odd_semester_start','odd_semester_end','even_semester_start','even_semester_end','active_semester');
		$crud->display_as('school_years','Tahun Ajaran');
		$crud->display_as('odd_semester_start','Tgl Mulai Semester Ganjil');
		$crud->display_as('odd_semester_end','Tgl Selesai Semester Ganjil');
		$crud->display_as('even_semester_start','Tgl Mulai Semester Genap');
		$crud->display_as('even_semester_end','Tgl Selesai Semester Genap');
		$crud->required_fields('school_years','odd_semester_start','odd_semester_end','even_semester_start','even_semester_end');
		//$crud->fields('school_years','odd_semester_start','odd_semester_end','even_semester_start','even_semester_end');
		
		$crud->callback_before_insert(array($this, 'check_active_semester'));
		$crud->callback_after_update(array($this, 'check_active_semester'));
		
		if( $operation == 'insert_validation' || $operation == 'insert')
			$crud->set_rules('school_year', 'school_year','callback_school_year_check');
		else
			$crud->set_rules('school_year', 'school_year','callback_school_year_update_check');
			
        $output = $crud->render();
        $this->_crud_output($output);	
	}
	
	public function check_active_semester($post_array){
		if( $post_array['active_semester'] > 0) {
			$id = $last = $this->uri->segment($this->uri->total_segments());
			$this->school_years->deactivate_all_semester_except_current($id);
		}
		else
			$post_array['active_semester'] = 0;
		return $post_array;
	}
	
	public function school_year_check($str)
	{
		$rs = $this->school_years->get_by_name($str);
		if( count($rs)>0 ) 
		{
			$this->form_validation->set_message('school_year_check', 'Nama tahun ajaran sudah ada.');
			return FALSE;
		}
		else
			return TRUE;
	}
	
	public function school_year_update_check($str)
	{		
		$id = $last = $this->uri->segment($this->uri->total_segments());
		
		if(!empty($id) && is_numeric($id))
		{
		   $rs = $this->school_years->get_by_id($id);
		   $old = $rs[0]['school_year'];
		}
		if($old==$str)
			return TRUE;
		else{
			$rs = $this->school_years->get_by_name($str);
		}
		if (count($rs) > 0)
		{
			$this->form_validation->set_message('school_year_update_check', 'Update gagal, tahun ajaran sudah ada');
		    return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
}
